﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Domain.NetworkRemoteDevice;
using Domain.NetworkService.Behaviors.Interfaces;

namespace Domain.NetworkService
{
    public class TestsSimpleFabric
    {
        public INetworkServiceTest Test { get; set; }
        public bool MakeTest(IDescription description)
        {
            if (description.networkTestDescription.Equals(NetworkTestDescription.ECHO))
            {
                IPAddress ip;
                if (IPAddress.TryParse(description.HostNameOrIP, out ip))
                {
                    EchoDescription echoDesc = description as EchoDescription;
                    Test = new EchoTestBehavior(ip, description.Timeout, 
                                                echoDesc.Size==0?32:echoDesc.Size, 
                                                echoDesc.DontFragment, 
                                                echoDesc.ResponseTime==0?5000:echoDesc.ResponseTime, 
                                                echoDesc.Ttl==0?128:echoDesc.Ttl);
                    return true;
                }
            }
            if (description.networkTestDescription.Equals(NetworkTestDescription.NS))
            {
                Test = new ResolveTestBehavior(description.HostNameOrIP, description.Timeout);
                return true;
            }
            if (description.networkTestDescription.Equals(NetworkTestDescription.SNMP))
            {
                IPAddress ip;
                if (IPAddress.TryParse(description.HostNameOrIP, out ip))
                {
                    OidNames oid = ((OidNames)(description.Special));
                    Test = new SnmpTestBehavior(ip, description.Timeout, oid); 
                    return true;
                }
            }
            if (description.networkTestDescription.Equals(NetworkTestDescription.TCP))
            {
                IPAddress ip;
                if (IPAddress.TryParse(description.HostNameOrIP, out ip))
                {
                    TcpPorts p = (TcpPorts)description.Special;
                    Test = new TcpTestBehavior(ip, description.Timeout, p);
                    return true;
                }
            }
            return false;
        }
    }
}
